package org.fengling.readingplatform.novel.controller;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.fengling.readingplatform.model.entity.novel.Novel;
import org.fengling.readingplatform.model.entity.novel.NovelFeedback;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.fengling.readingplatform.novel.factory.HandlerSortStrategyFactory;
import org.fengling.readingplatform.novel.factory.SortFactory;
import org.fengling.readingplatform.novel.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value="/api/novel")
public class NovelController {

    @Autowired
    private NovelService novelService;

    @Operation(summary = "分页查询")
    @GetMapping(value="/novelList/{page}/{limit}")
    public Result<PageInfo<Novel>> findByPage(@PathVariable Integer page,
                                              @PathVariable Integer limit,
                                              String category,
                                              String strategy) {
        System.out.println(strategy);
        HandlerSortStrategyFactory invokeStrategy = SortFactory.getInvokeStrategy(strategy);
        PageInfo<Novel> pageInfo = invokeStrategy.findByPage(page, limit, category);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "小说详情")
    @GetMapping(value="/getNovelInfo/{novelId}")
    public Result<Novel> getNovelInfo(@PathVariable Long novelId){
        Novel novel = novelService.getNovelInfo(novelId);
        return Result.build(novel, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "小说清单")
    @GetMapping(value="/getNovel/{novelId}")
    public Novel getNovel(@PathVariable Long novelId){
        return novelService.getNovelInfo(novelId);
    }

    @Operation(summary = "小说反馈")
    @GetMapping(value = "/getNovelFeedback/{novelId}")
    public Result<NovelFeedback> getNovelFeedback(@PathVariable Long novelId){
        NovelFeedback novelFeedback = novelService.getNovelFeedback(novelId);
        return Result.build(novelFeedback, ResultCodeEnum.SUCCESS);
    }

    @PutMapping(value = "/auth/like/{novelId}")
    public Result likeNovel(@PathVariable("novelId") Long novelId){
        novelService.likeNovel(novelId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //下载
    @GetMapping("/download/{novelId}")
    public Result download(@PathVariable("novelId") Long novelId){
        System.out.println(novelId);
        novelService.download(novelId);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}